<template>
  <div
    class="emoji_box"
    :style="{
      display: emojiBoxShow ? 'block' : 'none',
      position: `absolute`,
      left: `${left}px`,
      top: `${top}px`,
      zIndex: `10001`,
    }"
    @click.stop="() => {}"
  >
    <div class="emoji_title">
      {{ currentEmojiList.title }}
    </div>
    <div class="emoji_wrap">
      <a
        class="emoji_list emoji_text"
        :class="emoji.text ? 'emoji_default' : ''"
        :data-emoji-text="emoji.title ? emoji.title : emoji.text"
        :data-pid="currentEmojiList.pid"
        :data-id="emoji.id"
        :data-index="index"
        v-for="(emoji, index) in currentEmojiList.emojis"
        @click="inputEmoji(emoji)"
        @mousedown.prevent="() => {}"
      >
        {{ emoji.text }}
        <img :src="emoji.imgUrl" :title="emoji.title" v-if="emoji.imgUrl" />
      </a>
    </div>
    <div class="emoji_tabs">
      <div class="emoji_tab_container">
        <div
          class="tab_link"
          v-for="(item, index) in emojiList"
          :key="index"
          :class="index == emojiTabActive ? 'on' : ''"
          @click="emojiTabSwitch(index)"
          @mousedown.prevent="() => {}"
        >
          <img :src="item.imgUrl" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import EMOJILIST from "../../static/emoji.js";
export default {
  data() {
    return {
      emojiList: EMOJILIST,
      emojiTabActive: 0, //当前表情tab页
      currentEmojiList: [],
      currentInputNode: null, //当前指向输入框
      top: 0,
      left: 0,
    };
  },
  props: {
    emojiBoxShow: {
      typeof: Boolean,
      required: false,
      default: false,
    },
  },
  mounted() {
    this.$set(this, "currentEmojiList", this.emojiList[this.emojiTabActive]);
  },
  methods: {
    // 插入表情
    inputEmoji(emoji) {
      this.currentInputNode.focus();
      this.$bus.$emit("inputEmoji", emoji);
    },
    // 表情菜单切换
    emojiTabSwitch(index) {
      if (this.emojiTabActive != index) {
        this.$set(this, "emojiTabActive", index);
        this.$set(this, "currentEmojiList", this.emojiList[index]);
      }
    },
  },
  watch: {
    emojiBoxShow(newVal) {
      if (newVal) {
        this.$bus.$on("getPosition", (top, left) => {
          this.top = top;
          this.left = left;
        });
        this.$bus.$on("getNode", (node) => {
          this.currentInputNode = node;
        });
      } else {
        this.$bus.$off("getNode");
        this.$bus.$off("getPosition");
        this.currentInputNode = null;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.emoji_box {
  margin: 7px 0 10px;
  border: 1px solid #e5e9ef;
  box-shadow: 0 11px 12px 0 rgba(106, 115, 133, 0.3);
  border-radius: 8px;
  width: 363px;
  background-color: #fff;
  font-size: 12px;
  font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif;
  // overflow: hidden;
  box-sizing: content-box;
  position: relative;
  &::before {
    content: "";
    width: 10px;
    height: 10px;
    position: absolute;
    transform: rotate(45deg);
    border-radius: 4px 0 0 0;
    background-color: #fff;
    top: -5px;
    left: 20px;
    border: 1px solid #e5e9ef;
  }
  &::after {
    content: "";
    width: 15px;
    height: 8px;
    position: absolute;
    top: 0px;
    left: 19px;
    background-color: #fff;
  }
  .emoji_title {
    margin: 13px 15px 0;
  }
  .emoji_wrap {
    margin: 6px 11px 0;
    height: 195px;
    overflow: auto;
    word-break: break-word;
    .emoji_list {
      margin: 4px;
      line-height: 0;
      img {
        width: 24px;
        height: 24px;
      }
    }
    .emoji_default {
      font-size: 12px;
      line-height: 22px;
    }
    .emoji_text {
      margin: 2px 3px;
      padding: 3px 5px;
      display: inline-block;
      border-radius: 4px;
      cursor: pointer;
      transition: background 0.2s;
      &:hover {
        background-color: #ddd;
      }
    }
  }
  .emoji_tabs {
    height: 36px;
    background-color: #f4f4f4;
    border-radius: 0 0 4px 4px;
    overflow: hidden;
    .emoji_tab_container {
      display: flex;
      flex-direction: row;
      .tab_link {
        padding: 7px 18px;
        cursor: pointer;
        &.on {
          background-color: #fff;
        }
        &:hover {
          background-color: #e7e7e7;
        }
        img {
          width: 22px;
          height: 22px;
        }
      }
    }
  }
}
</style>